<template>
  <div class="course-user-page">
    <div class="head">全部评论</div>
<!--    条件筛选-->
    <div class="select-button">
      <el-row>
        <el-col :span="10">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-input v-model="inputComment" placeholder="请输入评论内容关键词"></el-input>
            </el-col>
            <el-col :span="12">
              <el-input v-model="inputUserId" placeholder="请输入用户id"></el-input>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12" :offset="1">
          <el-row :gutter="20">
            <el-col :span="10">
              <el-input v-model="inputSectionContentName" placeholder="请输入课时名称"  ></el-input>
            </el-col>
            <el-col :span="5" :offset="1">
              <el-button type="primary" plain @click="pageComments()" >查询</el-button>
            </el-col>
            <el-col :span="5" :offset="1">
              <el-button type="primary" plain @click="clear()" >清空</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
<!--    表单table-->
    <div class="student-list" style="display: flex;flex-direction: column;align-items: center">
      <el-table :data="tableData" height="150" border style="width: 100%">
        <el-table-column type="selection" width="55"align="center">
        </el-table-column>
        <el-table-column fixed prop="id" label="id" min-width="50%" align="center">
        </el-table-column>
        <el-table-column prop="sectionName" label="小节" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="nickname" label="用户名" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="content" label="内容" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="reply" label="回复" min-width="40%" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{scope.row.reply.replace(/<[^>]+>/g,"")}}
          </template>
        </el-table-column>
        <el-table-column prop="visibilityText" label="可视权限" min-width="40%" align="center" >

        </el-table-column>
        <el-table-column prop="gmtCreate" label="发布时间" min-width="55%" align="center" >
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="100px">
          <template slot-scope="scope">
            <el-button type="text" size="medium" @click="showVisibilityModel(scope.row.id,scope.$index)">编辑</el-button>
            <el-button type="text" size="medium" @click="showReplyModel(scope.row.id,scope.$index)">回复</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block" style="margin-top: 20px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[8, 10, 20, 40]"
          :page-size="8"
          layout="total, sizes, prev, pager, next, jumper"
          :total="parseInt(total)">
        </el-pagination>
      </div>
    </div>
<!--    可视权限修改弹窗表单-->
    <el-dialog title="操作" show-close :visible.sync="visibilityVisible">
      <el-form label-width="120px">
        <el-form-item label="可视权限">
          <el-select v-model="visibilityType" filterable style="min-width: 450px">
            <el-option
              v-for="(item,index) in visibilityTypes"
              :key="item"
              :label="item"
              :value="index">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="visibilityVisible = false">取 消</el-button>
        <el-button type="primary" @click="upgrade">确 定</el-button>
      </div>
    </el-dialog>
<!--    回复弹窗表单-->
    <el-dialog title="操作" show-close :visible.sync="replyVisible">
      <el-form label-width="120px">
        <el-form-item label="评论回复">
          <WangEditor :intro-html="inputReply" @setIntroduction="setIntroduction"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="replyVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import WangEditor from "@/components/WangEditor.vue";
export default {
  name: "StudentManageView",
  components : {WangEditor},
  data() {
    return {
      total: 0,
      currentSize: 8,
      currentPage: 1,
      visibilityType:0,
      visibilityTypes:["仅自己可见","仅关注可见","全部可见","经典问题"],

      commentId : "",
      auditId : "",
      courseId : "",

      inputReply:"",
      inputComment: "",
      inputUserId: "",
      inputSectionContentName:"",

      visibilityVisible : false,
      replyVisible : false,

      tableData: [],
    };
  },
  mounted() {
    this.courseId = this.$route.params.id
    this.commentId = this.$route.query.linkId
    this.auditId = this.$route.query.auditId
    this.pageComments();
  },
  methods: {
    pageComments() {
      this.$api.courseCommentsPage({
        pageSize: this.currentSize,
        pageNow: this.currentPage,
        sectionContentName: this.inputSectionContentName || '',
        sectionContentComment: this.inputComment || '',
        userId: this.inputUserId || '',
        id : this.commentId || '',
        courseId : this.courseId
      })
        .then((res) => {
          let list = res.data.rows;
          this.total = res.data.totalPage
          console.log(this.total)
          this.tableData = list
        });
    },


    showVisibilityModel(commentId,index) {
      this.commentId = commentId
      this.visibilityVisible = true
      this.visibilityType = this.tableData[index].visibility
    },
    showReplyModel(commentId,index){
      this.currentBindCommentId = commentId
      this.replyVisible = true
      this.inputReply = this.tableData[index].reply
    },

    upgrade() {
      this.$api.upgrade({
        auditId: this.auditId,
        linkId : this.commentId,
        visibility: this.visibilityType,
        type : 6,// 对于后端AuditContentTypeEnum枚举值
      }).then(res => {
        console.log(res)
        this.visibilityVisible = false
        this.commentId = ''
        this.pageComments()
      })
    },

    update() {
      this.$api.commentUpdate({
        id: this.currentBindCommentId,
        reply:this.inputReply
      }).then(res => {
        console.log(res)
        this.replyVisible = false
        this.pageComments()
      })
    },

    clear(){
      this.inputComment = ''
      this.inputUserId = ''
      this.inputSectionContentName = ''
      this.pageComments()
    },

    setIntroduction: function (html) {
      this.inputReply = html;//在父组件修改值
    },

    handleSizeChange(val) {
      this.currentSize = parseInt(val);
      this.pageComments();
    },
    handleCurrentChange(val) {
      this.currentPage = parseInt(val);
      this.pageComments();
    },
  },

};
</script>

<style scoped lang="scss">

/deep/.el-table{
  .el-table__body-wrapper{
    height: calc(100% - 48px)!important; // 表格高度减去表头的高度
  }
}

.course-user-page{
  margin-top: 20px;
  width: 100%;
}


.head {
  padding-bottom: 10px;
}

.select-button {
  padding: 10px 0;
}

.student-list {
  width: 100%;
  padding: 10px 0;
  display: flex;
}
</style>
